package cn.zhiwen.reflectController;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo04 {
    public static void main(String[] args) {
        try {
            Class aClass = Class.forName("cn.zhiwen.entity.Student");

            System.out.println("***************获取所有的”公有“方法*******************");
            Method[] methods = aClass.getMethods();
            for (Method method : methods) {
                System.out.println(method);
            }

            System.out.println("***************获取所有的方法，包括私有的*******************");
            Method[] declaredMethods = aClass.getDeclaredMethods();
            for (Method declaredMethod : declaredMethods) {
                System.out.println(declaredMethod);
            }

            System.out.println("***************获取公有的show1()方法*******************");
            Method show1 = aClass.getMethod("show1", String.class);
            System.out.println(show1);
            Object o = aClass.getConstructor().newInstance();
            Object o1 = show1.invoke(o, "wuzhiwen");

            System.out.println("***************获取私有的show4()方法******************");
            Method show2 = aClass.getDeclaredMethod("show4", int.class);
            Object o2 = aClass.getConstructor().newInstance();
            show2.setAccessible(true);
            Object invoke = show2.invoke(o2,23);
            System.out.println(invoke);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
